<script type="text/ecmascript-6">
  import { mapGetters } from 'vuex'
  export default{
    name:"chub_activityList",
    data(){
      return{
        tabList:[],
//             dataList:{
//               0: [],
//               1: [],
//               2: [],
//               3: [],
//               4: [],
//               5: [],
//               6: []
//             },
        dataList:{},
        id: this.$route.params.id,
        tabIndex:0,
        flex:false,
        activity_type: '',
        types:[],
        heights:"100%",
        isDrag:false,
        title:""
      }
    },
    methods:{
      goDrupel(id){
          window.location.href='https://www.thenorthface.com.cn/activity/view/'+id;

      },
      loadMore(){
//          if(!this.tabList[this.tabIndex].hasMore){
//              return this.$emit("done");
//          }
        const page =this.tabList[this.tabIndex].page+1;
        this.$store.dispatch('queryChubListActivity',{number:this.id,type:this.tabList[this.tabIndex].activity_type,title:this.tabList[this.tabIndex].name,index:this.tabIndex,page:page});
      },
      refresh(){
        this.$store.dispatch('queryChubListActivity',{number:this.id,type:this.tabList[this.tabIndex].activity_type,title:this.tabList[this.tabIndex].name,index:this.tabIndex});
      },
      go(id){
        this.$router.push({name:'activity_detail',params:{id:id,entrance:"registration"}})
//          window.location.href="http://m.dev.tnf.ibrand.cc/activity/#!/registration/activity_detail/"+id;
      },
      done(){
        this.$nextTick(() => {
          this.$refs.pulldown[this.tabIndex].onLoadOff();
        })
      },
      change(i){
        const type=this.tabList[i].activity_type;
        this.tabIndex=i;
        const url = '#/chub/listActivity/'+type;
        let title=this.tabList[i].name;
        console.log(title);
        window.history.replaceState('chub_listActivity','文章列表',url);
        if( !this.tabList[i].init ){
          this.$Indicator.blade();
          this.$store.dispatch('queryChubListActivity',{number:this.id,type:type,title:title,index:i});
        }
        else{
          this.tabList[i].noList = !!!this.dataList[i].length;
        }
      },
      jump_detail(id) {
        this.$router.push({ name: 'chub_detail', params: {id: id} });

      },
      setTitle(title){

        this.title=title;
      },
      CompleteChubListActivity(res,index){
        let list,
          page = res.meta.pagination,
          current_page = page.current_page,
          total_pages =page.total_pages;
        if( current_page ==1){
          list =res.data;
        }else{
          list = this.dataList[index].concat(res.data);
        }
        console.log(index);
//           this.dataList[index] =list;
        this.$set(this.dataList,index,list);
        this.tabList[index].init=true;
        this.tabList[index].page=current_page;
        this.tabList[index].hasMore = current_page < total_pages;
        this.tabList[index].noList = !!!list.length;
        this.$emit("done");
      },
      CompleteChubDrupelList(res,index){
        let list,
          current_page,
          total_pages;
        if(res.meta){
            current_page=Number(res.meta.page),
            total_pages=Math.ceil(res.meta.count/14);
        }
        else{
          current_page=1;
          total_pages=1
        }
        if( current_page===1){
          list= res.data?res.data:[];
        }
        else{
          list= this.dataList[index].concat(res.data);
        }
        this.$set(this.dataList,index,list);
        this.tabList[index].init = true;
        this.tabList[index].page = current_page;
        this.tabList[index].hasMore = current_page < total_pages;
        this.tabList[index].noList = !!!list.length;
        this.$emit("done");
      },
      CompleteActivityType(res){
        this.activity_type = res;
        this.activityType();
      },
      activityType(){
        const that=this;
        this.tabList=[

        ];
        that.activity_type.forEach( (val,i)=>{
          let o={
            page:0,
            hasMore:true,
            init:false,
            noList:false
          };
          o.name=val.name;
          o.activity_type=val.id;
          that.tabList.push(o);
          that.types.push(val.id);
        });
        let type = Number(this.$route.params.type);
        if( type!=0 ){
          this.tabIndex = this.types.indexOf(type);
        }
//        let id =  Number(this.$route.params.id);
        let title=this.tabList[this.tabIndex].name;
        this.$store.dispatch('queryChubListActivity',{type:type,title:title,index:this.tabIndex});
      }
    },
    created(){
      this.$Indicator.blade();
      EventBus.$on("complete-chubListActivity",this.CompleteChubListActivity);
      EventBus.$on("complete-chubDrupelActivity",this.CompleteChubDrupelList);
      EventBus.$on("complete-activity-type",this.CompleteActivityType);
      EventBus.$on("set-Listtitle",this.setTitle);
      this.$on("done",this.done);
      this.$store.dispatch('queryChutActivityType');
    },
    beforeDestroy(){
      EventBus.$off("complete-chubListActivity");
      EventBus.$off("complete-chubDrupelActivity");
      EventBus.$off("complete-activity-type");
      EventBus.$off("set-Listtitle");
    },
    mounted(){
//      this.$store.dispatch('queryChubList',number,type);
    }
//    activated(){
//      EventBus.$on("complete-chubList",this.CompleteChubList);
//      EventBus.$on("complete-activity-type",this.CompleteActivityType);
//      EventBus.$on("set-title",this.setTitle);
//      this.$store.dispatch('queryChutActivityType');
//    }
  }
</script>
